package com.fw4j.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Map;
import java.util.Set;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public abstract class PagerJump extends HttpServlet {

	//实例化一个FreeMaker模板的Configuration实例  
	protected static Configuration cfg = null; 
	protected static String jspPath = "";
	private static final long serialVersionUID = 1L;

	public PagerJump() {
		super();
	}

	protected void jumpPage(HttpServletRequest request, HttpServletResponse response, Object retVal,
			 OutParameter<String,Object> outparam) throws IOException {
				if(retVal instanceof String){
					StringBuilder path = new StringBuilder();
					path.append(String.valueOf(retVal));
					if(path.lastIndexOf(".ftl") > 0){
						ftlTemplate(request, response, path,outparam);
					}else if(path.lastIndexOf(".jsp")>0){
						path.insert(0, jspPath);
						jspTemplate(request, response, path,outparam);
					}else{
						jspTemplate(request, response, path,outparam);
					}
				}else if(retVal instanceof Collection<?>){
					try{
						PrintWriter out = response.getWriter();
						out.print(JSONArray.fromObject(retVal).toString());
					}catch(Exception ex){}
				}else if(retVal instanceof Map){
					try{
						PrintWriter out = response.getWriter();
						out.print(JSONObject.fromObject(retVal).toString());
					}catch(Exception ex){}
				}//]if
			}

	private void ftlTemplate(HttpServletRequest request, HttpServletResponse response, StringBuilder path,
			OutParameter<String,Object> outparam) throws IOException {
				Template t = cfg.getTemplate(path.toString());
				response.setContentType("text/html;charset=" + t.getEncoding());
				try {
					Map<String,Object> data = outparam;
					t.process(data, response.getWriter());
				} catch (TemplateException e) {
					e.printStackTrace();
				}
			}

	private void jspTemplate(HttpServletRequest request, HttpServletResponse response, StringBuilder path,
			OutParameter<String,Object> outparam) throws IOException {
				if(outparam != null && outparam.size()>0){
					Set<String> keys = outparam.keySet();
					for(String key : keys){
						Object value = outparam.get(key);
						if(value instanceof Cookie){
							response.addCookie((Cookie)value);
						}else{
							request.setAttribute(key, outparam.get(key));
						}//]if
					}//]for
				}//]if
				
				if(path.length()>0 && path.charAt(0) == '/'){
					RequestDispatcher rd = request.getRequestDispatcher(path.toString());
					try{
						rd.forward(request, response);
					}catch(Exception ex){}
				}else if(path.length()>0 && path.indexOf("redirect:")> -1){
					response.sendRedirect(path.replace(path.indexOf("redirect:"), "redirect:".length(), "").toString());
				}//]if
			}

}